package com.mproject.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Locale;

import com.mproject.message.MessageUtil;
import com.mproject.message.ServerConfiguration;
import com.project.connection.DatabaseConnection;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

//Sets the path to base URL
@Path("/add")
public class Add {
	public static Connection con = DatabaseConnection.getConnection();

	// ADD A USER IN A PROJECT
	@GET
	@Path("/project")
	@Produces("text/plain")
	public String addProject(@QueryParam("nusnet") String nusnet,
			@QueryParam("project_id") String project_id) {
		String result = "{\"Results\":[";

		try {
			Statement stmt = con.createStatement();
			String query = "select id from project where nusnet = '" +nusnet+ "' and project_id = '" +project_id+ "';";
			ResultSet rs = stmt.executeQuery(query);

			if(!rs.next()) {				
				String update = "insert into project (nusnet, project_id) values ('" +nusnet+ "', '" +project_id+ "');";
				stmt.executeUpdate(update);			

				query = "select id from project where nusnet = '" +nusnet+ "' and project_id = '" +project_id+ "';";
				rs = stmt.executeQuery(query);

				while(rs.next()) {
					result += "{\"id\":" + rs.getString("id") +"}";
				}
			}

		} catch (Exception e) {
			System.out.println(e);
		}

		result += "]}";
		return result;
	}

	// ADD A BUSY HOUR
	@GET
	@Path("/busy")
	@Produces("text/plain")
	public String addBusyHour(@QueryParam("project_id") String project_id,
			@QueryParam("date") long date,
			@QueryParam("block") int block,
			@QueryParam("nusnet") String nusnet) {
		String result = "{\"Results\":[";

		try {
			Statement stmt = con.createStatement();
			String query = "select id from busy where project_id = '" +project_id+ "' and date = " +date+ " and block = " +block+ " and nusnet = '" +nusnet+ "';";
			ResultSet rs = stmt.executeQuery(query);

			if(!rs.next()){
				String update = "insert into busy (project_id, date, block, nusnet) values ('" +project_id+ "', '" +date+ "', '" +block+ "', '" +nusnet+ "');";
				stmt.executeUpdate(update);			

				query = "select id from busy where project_id = '" +project_id+ "' and date = " +date+ " and block = " +block+ " and nusnet = '" +nusnet+ "';";
				rs = stmt.executeQuery(query);

				while(rs.next()) {
					result += "{\"id\":" + rs.getString("id") +"}";
				}
			}


			while(rs.next()) {
				result += "{\"id\":" + rs.getString("id") +"}";
			}

		} catch (Exception e) {
			System.out.println(e);
		}

		result += "]}";
		return result;
	}

	// ADD A TASK
	@GET
	@Path("/task")
	@Produces("text/plain")
	public String addTask(@QueryParam("project_id") String project_id,
			@QueryParam("project_name") String project_name,
			@QueryParam("due") long due,
			@QueryParam("date") long date,
			@QueryParam("title") String title,
			@QueryParam("des") String des,
			@QueryParam("nusnet") String nusnet) {
		String result = "{\"Results\":[";

		try {
			Statement stmt = con.createStatement();
			String query = "select id from work where project_id = '" +project_id+ "' and project_name = '" +project_name+ "' and cat = 'task' and due = " +due+ " and date = " +date+ " and title = '" +title+ "' and des = '" +des+ "';";
			ResultSet rs = stmt.executeQuery(query);

			if(!rs.next()) {				
				String update = "insert into work (project_id, project_name, cat, start, due, date, title, des) values ('" +project_id+ "', '" +project_name+ "', 'task', '" +due+ "', " +due+ ", " +date+ ", '" +title+ "', '" +des+ "');";
				stmt.executeUpdate(update);			

				query = "select id from work where project_id = '" +project_id+ "' and project_name = '" +project_name+ "' and cat = 'task' and due = " +due+ " and date = " +date+ " and title = '" +title+ "' and des = '" +des+ "';";
				rs = stmt.executeQuery(query);

				while(rs.next()) {
					result += "{\"id\":" + rs.getString("id") +"}";
				}
			}

		} catch (Exception e) {
			System.out.println(e);
		}

		result += "]}";

		String send_message = "not{\"message\":{\"cat\":\"add_task\",\"project_name\":\""+project_name+"\",\"start\":"+due+",\"title\":"+title+"}}";
		try {
			Statement stmt = con.createStatement();
			String query = "select u.mac from user u, project p where u.nusnet = p.nusnet and u.nusnet <> '" +nusnet+ "' and p.project_id = '" +project_id+ "';";
			ResultSet rs = stmt.executeQuery(query);
			while(rs.next()) {
				MessageUtil.sendMessage(ServerConfiguration.AUTHENTICATION_TOKEN,rs.getString("mac"),send_message);
			}

		} catch (Exception e) {
			System.out.println(e);
		}	

		return result;
	}

	// ADD A MEETING
	@GET
	@Path("/meeting")
	@Produces("text/plain")
	public String addMeeting(@QueryParam("project_id") String project_id,
			@QueryParam("project_name") String project_name,
			@QueryParam("start") long start,
			@QueryParam("end") long end,
			@QueryParam("date") long date,
			@QueryParam("title") String title,
			@QueryParam("des") String des,
			@QueryParam("venue") String venue,
			@QueryParam("nusnet") String nusnet) {
		String result = "{\"Results\":[";

		try {
			Statement stmt = con.createStatement();
			String query = "select id from work where project_id = '" +project_id+ "' and project_name = '" +project_name+ "' and cat = 'meeting' and start = " +start+ " and end = " +end+ " and date = " +date+ " and title = '" +title+ "' and des = '" +des+ "' and venue = '" +venue+ "';";
			ResultSet rs = stmt.executeQuery(query);

			if(!rs.next()) {				
				String update = "insert into work (project_id, project_name, cat, start, end, date, title, des, venue) values ('" +project_id+ "', '" +project_name+ "', 'meeting', " +start+ ", " +end+ ", " +date+ ", '" +title+ "', '" +des+ "', '" +venue+ "');";
				stmt.executeUpdate(update);			

				query = "select id from work where project_id = '" +project_id+ "' and project_name = '" +project_name+ "' and cat = 'meeting' and start = " +start+ " and end = " +end+ " and date = " +date+ " and title = '" +title+ "' and des = '" +des+ "' and venue = '" +venue+ "';";
				rs = stmt.executeQuery(query);

				while(rs.next()) {
					result += "{\"id\":" + rs.getString("id") +"}";
				}
			}

		} catch (Exception e) {
			System.out.println(e);
		}

		result += "]}";

		String send_message = "not{\"message\":{\"cat\":\"add_meeting\",\"project_name\":\""+project_name+"\",\"start\":"+start+",\"title\":"+title+"}}";
		try {
			Statement stmt = con.createStatement();
			String query = "select u.mac from user u, project p where u.nusnet = p.nusnet and u.nusnet <> '" +nusnet+ "' and p.project_id = '" +project_id+ "';";
			ResultSet rs = stmt.executeQuery(query);
			while(rs.next()) {
				MessageUtil.sendMessage(ServerConfiguration.AUTHENTICATION_TOKEN,rs.getString("mac"),send_message);
			}

		} catch (Exception e) {
			System.out.println(e);
		}	

		return result;
	}

	// ADD A MESSAGE
	@GET
	@Path("/message")
	@Produces("text/plain")
	public String addMessage(@QueryParam("from") String from,
			@QueryParam("to") String to,
			@QueryParam("message") String message,
			@QueryParam("project_id") String project_id,
			@QueryParam("date") int date) {
		String result = "{\"Results\":[";

		try {
			Statement stmt = con.createStatement();
			String update = "insert into message (from_id, to_id, message, project_id, date) values ('" +from+ "', '" +to+ "', " +message+ ", " +project_id+ ", '" +date+ "')";
			stmt.executeUpdate(update);			

			String query = "select id from message where from_id = '" +from+ "' and to_id = '" +to+ "' and message = " +message+ " and project_id = " +project_id+ " and date = '" +date+ "')";
			ResultSet rs = stmt.executeQuery(query);

			while(rs.next()) {
				result += "{\"id\":" + rs.getString("id") +"}";
			}

		} catch (Exception e) {
			System.out.println(e);
		}

		result += "]}";
		return result;
	}

	// ADD A PLAN
	@GET
	@Path("/plan")
	@Produces("text/plain")
	public String addPlan(@QueryParam("title") String title,
			@QueryParam("des") String des,
			@QueryParam("start") long start,
			@QueryParam("end") long end,
			@QueryParam("incharged") String incharged,
			@QueryParam("project_id") String project_id) {
		String result = "{\"Results\":[";

		try {
			Statement stmt = con.createStatement();
			String update = "insert into plan (title, des, start, end, incharged, project_id) values ('" +title+ "', '" +des+ "', " +start+ ", " +end+ ", '" +incharged+ "', '" +project_id+ "')";
			stmt.executeUpdate(update);			

			String query = "select id from plan where title = '" +title+ "' and des = '" +des+ "' and start = " +start+ " and end = " +end+ " and incharged = '" +incharged+ "'and project_id = '" +project_id+ "'";
			ResultSet rs = stmt.executeQuery(query);

			while(rs.next()) {
				result += "{\"id\":" + rs.getString("id") +"}";
			}

			result += "]}";
		} catch (Exception e) {

			System.out.println(e);
		}
		return result;
	}

}